package jDialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import main.Group;

public class RandomGroups extends JDialog {

	private static final long serialVersionUID = 1L;

	private ArrayList<String> listInfo;
	private ArrayList<Group> listGroups = new ArrayList<Group>();
	private ArrayList<JComboBox<String>> comboList = new ArrayList<JComboBox<String>>();;
	private String path, fileName;

	private JButton bttnCancel, bttnNext;
	private JPanel pBottom, pCenter;
	private JLabel students, groupLabel;
	private JRadioButton numStudent, numGroups;
	private JSpinner spinner1;

	public RandomGroups(JFrame frame, ArrayList<String> listInfo, String path, String fileName) {
		super(frame, "Create Groups", ModalityType.DOCUMENT_MODAL);

		this.path = path;
		this.fileName = fileName.substring(0, fileName.indexOf("."));
		this.listInfo = listInfo;
		Random gen = new Random();
		for(int i=0; i <= gen.nextInt(100); i++)
			Collections.shuffle(this.listInfo);

		this.initPanel();
		this.initButton();
		this.pack();
		this.setDialogPos();
		this.setResizable(true);
		this.setVisible(true);
	}

	private void addGroupPanel() {
		this.getContentPane().removeAll();
		JPanel pGroup = new JPanel(new GridLayout(0, 3));
		for (int a = 0; a < listGroups.size(); a++)
			pGroup.add(listGroups.get(a).getGroup());
		this.add(pGroup, BorderLayout.NORTH);
	}

	private void addPanels_2() {
		JPanel pNoGroup = new JPanel(new GridLayout(0, 2));
		pNoGroup.add(new JLabel("<html><u>Name</u></html>"));
		pNoGroup.add(new JLabel("<html><u>Select Group To Be Put In</u></html>"));

		String[] obj = new String[listGroups.size() + 1];
		for (int i = 0; i <= listGroups.size(); i++)
			obj[i] = "Group: " + (i + 1);
		for (int a = 0; a < listInfo.size(); a++) {
			pNoGroup.add(new JLabel(listInfo.get(a)));
			JComboBox<String> combo = new JComboBox<String>(obj);
			combo.setSelectedIndex(0);
			comboList.add(combo);
			pNoGroup.add(combo);
		}
		JButton bttnQuit = new JButton("Cancel");
		bttnQuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				RandomGroups.this.dispose();
			}
		});

		JButton bttnNext_2 = new JButton("Next");
		bttnNext_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (int a = 0; a < comboList.size(); a++) {
					int index = comboList.get(a).getSelectedIndex();
					if (index + 1 > listGroups.size())
						listGroups.add(new Group(index + 1));
					listGroups.get(index).addToGroup(listInfo.get(a));
				}
				RandomGroups.this.addGroupPanel();
				RandomGroups.this.writeFile();
				RandomGroups.this.pack();
				RandomGroups.this.setDialogPos();
			}
		});
		pNoGroup.add(bttnNext_2);
		this.add(pNoGroup, BorderLayout.SOUTH);
	}

	private void initButton() {
		GridBagConstraints c = new GridBagConstraints();
		bttnCancel = new JButton("Cancel");
		bttnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		bttnNext = new JButton("Next");
		bttnNext.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				long seed = System.nanoTime();
				Collections.shuffle(listInfo, new Random(seed));

				int numOfGroups = 0, numOfStudentsInGroup = 0;
				if (numStudent.isSelected()) {
					numOfStudentsInGroup = (int) spinner1.getValue();
					numOfGroups = listInfo.size() / numOfStudentsInGroup;
				} else {
					numOfGroups = (int) spinner1.getValue();
					numOfStudentsInGroup = listInfo.size() / numOfGroups;
				}
				for (int a = 0; a < numOfGroups; a++) {
					Group g = new Group(a + 1);
					for (int b = 0; b < numOfStudentsInGroup; b++) {
						g.addToGroup(listInfo.get(0));
						listInfo.remove(0);
					}
					listGroups.add(g);
				}
				RandomGroups.this.addGroupPanel();
				if (listInfo.size() > 0)
					RandomGroups.this.addPanels_2();
				else
					RandomGroups.this.writeFile();
				RandomGroups.this.setTitle("Groups");
				RandomGroups.this.pack();
				RandomGroups.this.setDialogPos();
			}
		});

		c.anchor = GridBagConstraints.EAST;
		c.weightx = 1;
		c.insets.right = 10;
		c.insets.bottom = 7;
		pBottom.add(bttnCancel, c);

		c.insets.right = 25;
		c.weightx = 0;
		pBottom.add(bttnNext, c);
	}

	private void initComp() {
		students = new JLabel("Number of Students: " + listInfo.size());
		groupLabel = new JLabel("Select an option: ");

		SpinnerNumberModel model1 = new SpinnerNumberModel(2, 1, listInfo.size(), 1);
		spinner1 = new JSpinner(model1);

		ButtonGroup group = new ButtonGroup();
		numStudent = new JRadioButton("<html>Select the number of students<br>in each group</html>");
		numStudent.setSelected(true);
		group.add(numStudent);
		numGroups = new JRadioButton("Select the number of groups");
		group.add(numGroups);
	}

	private void initPanel() {
		int width = 300;
		this.initPCenter(width);

		pBottom = new JPanel(new GridBagLayout());
		pBottom.setPreferredSize(new Dimension(width, 40));
		this.add(pBottom, BorderLayout.PAGE_END);
	}

	private void initPCenter(int width) {
		pCenter = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		pCenter.setPreferredSize(new Dimension(width, 150));
		this.initComp();

		c.gridwidth = 2;
		c.weightx = 1;
		c.weighty = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.insets.left = 10;
		pCenter.add(students, c);

		c.gridwidth = 1;
		c.anchor = GridBagConstraints.WEST;
		c.gridy = 1;
		pCenter.add(groupLabel, c);

		c.insets.right = 10;
		c.gridx = 1;
		pCenter.add(spinner1, c);

		c.gridx = 0;
		c.gridy = 2;
		c.insets.right = 0;
		c.insets.left = 20;
		c.weighty = 0;
		pCenter.add(numStudent, c);
		c.gridy = 3;
		pCenter.add(numGroups, c);

		this.add(pCenter, BorderLayout.CENTER);
	}

	private void setDialogPos() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((d.getWidth() - this.getWidth()) / 2);
		int y = (int) ((d.getHeight() - this.getHeight()) / 2);
		this.setLocation(x, y);
	}

	private void writeFile() {
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String fileDir = path + "PastGroups/";
		String fileName = date + "__" + this.fileName + ".txt";

		File newfile = new File(fileDir, fileName);
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(newfile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		for (Group g : listGroups) {
			writer.print(g.toString() + "\n");
		}
		writer.close();
	}
}